package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/29 0029.
 */
@Controller
@RequestMapping(ControllerConstants.MANAGEMENT)
public class ManagementWController extends WebBaseController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private CarouselService carouselService;

    /**
     * 跳转推荐商家列表
     *
     * @return
     */
    @RequestMapping("/findRecommendBusinessIndex")
    public String findRecommendBusiness() {
        return "taoshunda/auditing/recommendBusiness";
    }

    /**
     * 跳转推荐广告位列表
     *
     * @return
     */
    @RequestMapping("/findRecommendAdvIndex")
    public String findRecommendAdv() {
        return "taoshunda/auditing/carousel";
    }

    /**
     * 查询推荐商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryRecommendBusiness")
    public void queryRecommendBusiness(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "businessName", "");
                String cityName = getString(request, "name", "");
                return businessService.queryRecommendBusiness(name, cityName, nowPage, pageSize);
            }
        });
    }

    /**
     * 删除推荐商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateIsRecommend")
    public void updateIsRecommend(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer isRecommend = getInt(request, "isRecommend", 0);
                return businessService.updateIsRecommend(id, isRecommend);
            }
        });
    }

    /**
     * 查询推荐广告位
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryRecommendAdv")
    public void queryRecommendAdv(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                Integer cityId = getInt(request, "cityId", 0);
                return businessService.queryRecommendAdv(name, cityId, nowPage, pageSize);
            }
        });
    }

    /**
     * 修改排名
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateOrderNum")
    public void updateNum(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer orderNum = getInt(request, "orderNum", 0);
                Integer sorting = getInt(request, "sorting", 0);
                return carouselService.updateOrderNum(id, orderNum, sorting);
            }
        });
    }

    /**
     * 更换图片
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updatePic")
    public void updatePic(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String picture = getString(request, "picture", "");
                return businessService.updatePic(id, picture);
            }
        });
    }
}
